package com.automation.ljf.strategy.impl;

import com.automation.ljf.exception.BizException;
import com.automation.ljf.repository.TestCaseRepository;
import com.automation.ljf.domain.dataobject.CaseStepDO;
import com.automation.ljf.strategy.OperationStrategy;
import com.automation.ljf.util.ResultMessage;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *  切换窗口
 *  (目前只能从在该步骤前面执行的步骤中选择步骤进行关联，然后获取关联步骤的窗口句柄id，跳转到关联步骤的窗口)
 *
 * @author lijiefeng
 * @since 2021/8/31
 */
@Component("SWITCH_WINDOW")
public class ToWindowsStrategyImpl implements OperationStrategy {

    @Autowired
    private TestCaseRepository testCaseRepository;

    @Override
    public ResultMessage doOperation(ChromeDriver driver, CaseStepDO caseStepDO, String jsonData) {
        //获取当前窗口句柄
        String windowHandle = driver.getWindowHandle();
        try{
            if (jsonData == null){
                throw new BizException(400, "切换的窗口id不能为NULL");
            }
            //切换
            driver.switchTo().window(jsonData);
        }catch (Exception e){
//            return ResultMessage.error("测试用例" + caseStepDO.getCaseId() + "的步骤" + caseStepDO.getStepId() + "出现异常")
//                    .addData("errorMsg", "切换窗口操作失败").addData("window", windowHandle);
            throw new BizException(400, "切换窗口操作失败");
        }
        return ResultMessage.ok("操作成功").addData("window", windowHandle);

    }

}
